# _*_ coding: UTF-8 _*_
# 2020/5/5 3:23 
# PyCharm  
# Create by:LIUMINXUAN
# E-mail:liuminxuan1024@gmail.com
import cv2
from pylab import *


def MedianFilter(Imge, dim):  # Image为待处理图像，dim为滤波器的大小dim*dim
    im = array(Imge)
    sigema = []
    for i in range(int(dim / 2), im.shape[0] - int(dim / 2)):
        for j in range(int(dim / 2), im.shape[1] - int(dim / 2)):
            for a in range(-int(dim / 2), -int(dim / 2) + dim):
                for b in range(-int(dim / 2), -int(dim / 2) + dim):
                    sigema.append(img[i + a, j + b])
            sigema.sort()
            im[i, j] = sigema[int(dim * dim / 2)]
            sigema = []
    return im


img = cv2.imread('./img/jiaoyan/danghui_jiaoyan.bmp', 0)
im3 = MedianFilter(img, 3)
im5 = MedianFilter(img, 5)
im7 = MedianFilter(img, 7)
cv2.imshow('zhongzhilvbo 3*3', im3)
cv2.imshow('zhongzhilvbo 5*5', im5)
cv2.imshow('zhongzhilvbo 7*7', im7)
cv2.imshow('Origin', img)
cv2.waitKey()
cv2.destroyAllWindows()
